﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

public partial class Manage_FileHandler : System.Web.UI.Page
{
    private static int nowpages;
    private int PageSize = 20;
    private int PageCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["ftb"]) || string.IsNullOrEmpty(Request.QueryString["path"]))
        {
            Response.Write("<script>window.opener=null;window.close();</script>");
        }
        this.Folder = Request.QueryString["path"];
        this.FTB = Request.QueryString["ftb"];
        if (!IsPostBack)
        {
            nowpages = 1;
            this.BingFile();
        }
    }
    private void BingFile()
    {
        FileInfo[] f = GetFiles.Get(Server.MapPath("~/" + this.Folder));
        if (f == null)
        {
            Response.Write("<script>window.opener=null;window.close();</script>");
        }
        ArrayList fp = new ArrayList();
        for (int i = PageSize * (nowpages - 1); (i < f.Length) && (i < PageSize * nowpages); i++)
        {
            fp.Add(f[i]);
        }
        filelist_gv.DataSource = fp;
        filelist_gv.DataBind();

        this.PageCount = (int)Math.Ceiling((double)f.Length / (double)PageSize);//计算一共有多少页

        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == this.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= this.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        nowpages_lb.Text = nowpages.ToString();
        pagecount_lb.Text = this.PageCount.ToString();
        tpagesizes.Text = this.PageSize.ToString();
        pagesum.Text = f.Length.ToString();
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.BingFile();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.BingFile();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.BingFile();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(pagecount_lb.Text);
        this.BingFile();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.BingFile();
    }
    protected void UpLoadFile_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(PicFileUpload.PostedFile.FileName))
        {
            string filepath = PicFileUpload.PostedFile.FileName;
            string fileextname = filepath.Substring(filepath.LastIndexOf(".") + 1);//扩展名
            if (!checkextname(fileextname))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('不支持这个格式的文件上传');</script>");
                return;
            }
            string mpath = Server.MapPath("~/" + this.Folder + "/");//文件夹路径
            string mfilename = filepath.Substring(0, (filepath.LastIndexOf(".")));
            try
            {
                PicFileUpload.PostedFile.SaveAs(mpath + mfilename + "." + fileextname);
                nowpages = 1;
                this.BingFile();
            }
            catch
            {

            }
        }
    }
    
    protected void filelist_gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string name = ((Label)filelist_gv.Rows[e.RowIndex].FindControl("Name_lb")).Text;
        string path = Server.MapPath("~/" + Folder + "/" + name);
        if (File.Exists(path))
        {
            try
            {
                File.Delete(path);
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除文件成功');</script>");
                this.BingFile();
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除文件失败');</script>");
            }

        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('文件不存在');</script>");
        }

    }
    private bool checkextname(string name)
    {
        string[] extname = { "zip", "rar", "pdf", "ppt", "pptx", "apk", "doc", "docx", "tar", "gz", "xls", "xlsx"};
        foreach(string e in extname)
        {
            if (name.Equals(e))
                return true;
        }
        return false;
    }
    protected string Folder
    {
        get;
        set;
    }
    protected string FTB
    {
        get;
        set;
    }

}